package com.tyut.controller;

import com.tyut.domain.MusicDocument;
import com.tyut.domain.TagDocument;
import com.tyut.domain.UserDocument;
import com.tyut.domain.dto.SearchRequest;
import com.tyut.domain.vo.SearchResult;
import com.tyut.musicdreamer.framework.starter.convention.result.Result;
import com.tyut.service.SearchService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 搜索控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/search")
@RequiredArgsConstructor
public class SearchController {

    private final SearchService searchService;

    /**
     * 搜索音乐
     *
     * @param request 搜索请求
     * @return 搜索结果
     */
    @PostMapping("/music")
    public Result<SearchResult<MusicDocument>> searchMusic(@RequestBody SearchRequest request) {
        log.info("搜索音乐: {}", request);
        return Result.success(searchService.searchMusic(request));
    }

    /**
     * 搜索用户
     *
     * @param request 搜索请求
     * @return 搜索结果
     */
    @PostMapping("/user")
    public Result<SearchResult<UserDocument>> searchUser(@RequestBody SearchRequest request) {
        log.info("搜索用户: {}", request);
        return Result.success(searchService.searchUser(request));
    }

    /**
     * 搜索标签
     *
     * @param request 搜索请求
     * @return 搜索结果
     */
    @PostMapping("/tag")
    public Result<SearchResult<TagDocument>> searchTag(@RequestBody SearchRequest request) {
        log.info("搜索标签: {}", request);
        return Result.success(searchService.searchTag(request));
    }

    /**
     * 综合搜索（根据type参数决定搜索类型）
     *
     * @param request 搜索请求
     * @return 搜索结果
     */
    @PostMapping
    public Result<?> search(@RequestBody SearchRequest request) {
        log.info("综合搜索: {}", request);
        
        switch (request.getType()) {
            case "music":
                return Result.success(searchService.searchMusic(request));
            case "user":
                return Result.success(searchService.searchUser(request));
            case "tag":
                return Result.success(searchService.searchTag(request));
            case "all":
            default:
                // 默认搜索音乐
                return Result.success(searchService.searchMusic(request));
        }
    }
    
    /**
     * 简单搜索
     * 
     * @param keyword 关键词
     * @param type 搜索类型（music, user, tag, all）
     * @param pageNum 页码
     * @param pageSize 每页大小
     * @return 搜索结果
     */
    @GetMapping("/simple")
    public Result<?> simpleSearch(
            @RequestParam("keyword") String keyword,
            @RequestParam(value = "type", defaultValue = "music") String type,
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        
        log.info("简单搜索: keyword={}, type={}, pageNum={}, pageSize={}", keyword, type, pageNum, pageSize);
        
        // 创建搜索请求
        SearchRequest request = new SearchRequest();
        request.setKeyword(keyword);
        request.setType(type);
        request.setPageNum(pageNum);
        request.setPageSize(pageSize);
        
        // 根据类型执行不同的搜索
        switch (type) {
            case "music":
                return Result.success(searchService.searchMusic(request));
            case "user":
                return Result.success(searchService.searchUser(request));
            case "tag":
                return Result.success(searchService.searchTag(request));
            case "all":
            default:
                // 默认搜索音乐
                return Result.success(searchService.searchMusic(request));
        }
    }
} 